//
//  AppDelegate.m
//  OASystem
//
//  Created by zhoupengfei on 15/3/23.
//  Copyright (c) 2015年 zpf. All rights reserved.
//
#import "MainViewController.h"
#import "AppDelegate.h"
#import "AppDelegate+Extendion.h"
#import "AnjiMainViewController_Second.h"
#import "OALoginViewController.h"
#import "AuthHelper.h"
#import "LockController.h"
@interface AppDelegate ()
<SangforSDKDelegate>
@property (nonatomic, strong) AuthHelper *helper;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
short port = 443;                        //vpn设备端口号，一般为443
NSString *vpnIp =    @"61.175.232.5";  //vpn设备IP地址
NSString *userName = @"app";             //用户名认证的用户名
NSString *password = @"appapp";                //用户名认证的密码

NSString *certName = @"";     //导入证书名字，如果服务端没有设置证书认证可以不设置
NSString *certPwd =  @"";          //证书密码，如果服务端没有设置证书
    
//统一设置导航栏颜色黑色
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] ;
     if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"shoushi"] isEqualToString:@"Y"]) {
  LockController *lockController = [[LockController alloc]init];
         self.window.rootViewController = lockController;
}else{
      UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"AnJiMain" bundle:nil];
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"autoLogin"] isEqualToString:@"Y"]) {
//        self.helper = [[AuthHelper alloc] initWithHostAndPort:vpnIp port:port delegate:self];
//    [self.helper setAuthParam:@AUTO_LOGIN_OFF_KEY param:@"true"];
      AnjiMainViewController_Second   * mainVC = [storyBoard instantiateViewControllerWithIdentifier:@"AnjiMainVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
        self.window.rootViewController = navVC;
        
    }else{
        OALoginViewController * loginVC = [storyBoard instantiateViewControllerWithIdentifier:@"LoginVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        self.window.rootViewController = navVC;
//          self.window.rootViewController = navVC;
    }
  
   }
    [self.window makeKeyAndVisible];
    [self dismissKeyBoard];
    return YES;
}
#pragma Mark VPNDelegate
- (void) onCallBack:(const VPN_RESULT_NO)vpnErrno authType:(const int)authType
{short port = 443;                        //vpn设备端口号，一般为443
NSString *vpnIp =    @"61.175.232.5";  //vpn设备IP地址
NSString *userName = @"app";             //用户名认证的用户名
NSString *password = @"appapp";                //用户名认证的密码

NSString *certName = @"";     //导入证书名字，如果服务端没有设置证书认证可以不设置
NSString *certPwd =  @"";          //证书密码，如果服务端没有设置证书
    switch (vpnErrno)
    {
        case RESULT_VPN_INIT_FAIL:
            NSLog(@"Vpn Init failed!");
            break;
            
        case RESULT_VPN_AUTH_FAIL:
            [self.helper clearAuthParam:@SET_RND_CODE_STR];
            NSLog(@"Vpn auth failed!");
            break;
            
        case RESULT_VPN_INIT_SUCCESS:
            NSLog(@"Vpn init success!");
            //设置认证参数 用户名和密码以数值map的形式传入
            [self.helper setAuthParam:@PORPERTY_NamePasswordAuth_NAME param:userName];
            [self.helper setAuthParam:@PORPERTY_NamePasswordAuth_PASSWORD param:password];
            //开始用户名密码认证
            [self.helper loginVpn:SSL_AUTH_TYPE_PASSWORD];
            break;
        case RESULT_VPN_AUTH_SUCCESS:
            [self startOtherAuth:authType];
            break;
        case RESULT_VPN_AUTH_LOGOUT:
            NSLog(@"Vpn logout success!");
            break;
        case RESULT_VPN_OTHER:
            if (VPN_OTHER_RELOGIN_FAIL == (VPN_RESULT_OTHER_NO)authType) {
                NSLog(@"Vpn relogin failed, maybe network error");
            }
            break;
            
        case RESULT_VPN_NONE:
            break;
            
        default:
            break;
    }
}

- (void) startOtherAuth:(const int)authType
{
short port = 443;                        //vpn设备端口号，一般为443
NSString *vpnIp =    @"61.175.232.5";  //vpn设备IP地址
NSString *userName = @"app";             //用户名认证的用户名
NSString *password = @"appapp";                //用户名认证的密码

NSString *certName = @"";     //导入证书名字，如果服务端没有设置证书认证可以不设置
NSString *certPwd =  @"";          //证书密码，如果服务端没有设置证书
    NSArray *paths = nil;
    switch (authType)
    {
            //            证书认证
        case SSL_AUTH_TYPE_CERTIFICATE:
            paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                        NSUserDomainMask, YES);
            
            if (nil != paths && [paths count] > 0)
            {
                NSString *dirPaths = [paths objectAtIndex:0];
                NSString *authPaths = [dirPaths stringByAppendingPathComponent:certName];
                NSLog(@"PATH = %@",authPaths);
                //P12证书路径
                [self.helper setAuthParam:@CERT_P12_FILE_NAME param:authPaths];
                //证书的密码
                [self.helper setAuthParam:@CERT_PASSWORD param:certPwd];
            }
            NSLog(@"Start Cert Auth!!!");
            break;
            
            /** 用户名密码认证 */
        case SSL_AUTH_TYPE_PASSWORD:
            NSLog(@"Start Password Name Auth!!!");
            [self.helper setAuthParam:@PORPERTY_NamePasswordAuth_NAME param:userName];
            [self.helper setAuthParam:@PORPERTY_NamePasswordAuth_PASSWORD param:password];
            
            break;
            /** 无认证 */
        case SSL_AUTH_TYPE_NONE:
            NSLog(@"Auth success!!!");
              UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        MainViewController * mainVC = [storyBoard instantiateViewControllerWithIdentifier:@"MainVC"];
        UINavigationController * navVC = [[UINavigationController alloc]initWithRootViewController:mainVC];
         self.window.rootViewController = navVC;

            return;
//        default:
//            NSLog(@"Other failed!!!");
//            return;
    }
    [self.helper loginVpn:authType];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        [_helper logoutVpn];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        [_helper logoutVpn];
}

@end
